<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/20
 * Time: 19:06
 */

namespace app\admin\controller;


use cmf\controller\AdminBaseController;

class PatientController extends AdminBaseController
{

    public function index()
    {

        $param = request()->param();
        $where = array('is_del'=>0);
        if (isset($param['start_time'])){
            if (trim($param['start_time']) != '' && trim($param['end_time']) != ''){
                $where['create_time'] = array('between', [strtotime($param['start_time']),strtotime($param['end_time'])]);
                //$where['create_time'] = array('lt', strtotime($param['end_time']));
            }elseif (trim($param['start_time']) != ''){
                $where['create_time'] = array('gt', strtotime($param['start_time']));
            }elseif (trim($param['end_time']) != ''){
                $where['create_time'] = array('lt', strtotime($param['end_time']));
            }
        }
        if (isset($param['keyword'])) {
            if ($param['keyword'] != '') {
                $where['name|phone|no'] = array('like', '%' . $param['keyword'] . '%');
            }
        }
        $this->assign('where', $param);

        $list = db('patient')->order("id DESC")->where($where)->paginate(20);
        //dump(db('patient')->getLastSql());
        $page = $list->render();
        $this->assign('patients', array());
        $this->assign('list', $list);
        $this->assign('page',$page);
        return $this->fetch();
    }
    public function addBase(){
        $data = request()->param();
        if(!empty($data['base'])){
            //查询临时表
            $today = strtotime(date("Y-m-d"),time());
            $where['create_time'] =['egt',$today];
            $num = db('tmp_order_log')->where($where)->count();
            $no = str_pad($num+1,4,0,STR_PAD_LEFT);
            if($data['base']['sex'] == 1){
                $no = 'm'.date('Ymd').$no;
            }else{
                $no = 'w'.date ('Ymd ').no;
            }
//dump($no);
           $map = [
                'name'=>$data['base']['name'],
               'age'=>$data['base']['age'],
               'nation'=>$data['base']['nation'],
               'sex'=>$data['base']['sex'],
               'phone'=>$data['base']['phone'],
               'address'=>$data['base']['address'],
               'medical_history'=>$data['base']['medical_history'],
               'allergy_history'=>$data['base']['allergy_history'],
               'create_time'=>time(),
               'no'=>$no,
               'parent_no'=>$data['base']['parent_no']
           ];
            $res = db('patient')->insert($map);
            if($res){
                $addlog = db('tmp_order_log')->insert(['create_time'=>time()]);
                if(!$addlog){
                    $this->error('添加日志失败');
                }
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
            return;
        }
        return $this->fetch();
    }
    public function add()
    {
        //查询用户信息
        $id = request()->param('id');
        $type = $this->request->param('type',1);
        if(empty($id)){
                $this->error('缺少参数');
                return;
        }
        $userinfo = db('patient')->where('id',$id)->find();
        $parentname = '';
        if($userinfo['parent_no']){
            $parentname = db('patient')->where('phone',$userinfo['parent_no'])->value('name');
            //$this->assign('parentname',$parentname);
        }
        //dump($userinfo['parent_no']);

        $this->assign('parentname',$parentname);
       $this->assign('userInfo',$userinfo);
        if($type == 1){
            return $this->fetch();
        }elseif ($type == 2){
            return $this->fetch('diabetes');
        }

    }
    //添加病例
    public function addcell(){
        $data = request()->param();
        $data['cell']['create_time'] = time();
        $res1 = db('patient_cell')->insertGetId($data['cell']);
        if(!empty($data['headImg'])){
            foreach($data['headImg'] as $k => $v){
                $res = db('imgs')->insert(['url'=>$v,'create_time'=>time(),'c_id'=>$res1]);
            }
        }
        //dump($data);die;

        if($res1){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }
//添加病例(糖尿病)
    public function diabetes(){
        $data = request()->param();
        $data['diabetes']['create_time'] = time();


        //是三维数组的的字段
        $arr = ['shenbingbingbian','dianxingzhengzhuang','xinnaoxueguanbingbian','shenjingxingbingbian',
            'yanbingbingbian','riqi','canqianxuetang','canhouxuetang_two','huatangfangyongliang','yaowufuyongliang',
        'yidaosuyongliang_two','xiaoyufangyongliang'];
        foreach($arr as $k=>$v){
            $is_kong = array_filter($data['diabetes'][$v]);
            if(!empty($is_kong)){
                $data['diabetes'][$v] = implode('--',$data['diabetes'][$v]);
            }
        }
        //dump($data['diabetes']);die;
        $res1 = db('patient_diabetes')->insertGetId($data['diabetes']);
        if(!empty($data['headImg'])){
            foreach($data['headImg'] as $k => $v){
                $res = db('imgs')->insert(['url'=>$v,'create_time'=>time(),'p_id'=>$res1]);
            }
        }

        if($res1){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }

    /**
     * 编辑病人基本信息
     */
    public function edit()
    {
        $data = request()->param();
        $id = $data['id'];
        unset($data['id']);
        if(!empty($data['base'])){
            $map = [
                'name'=>$data['base']['name'],
                'age'=>$data['base']['age'],
                'nation'=>$data['base']['nation'],
                'sex'=>$data['base']['sex'],
                'phone'=>$data['base']['phone'],
                'address'=>$data['base']['address'],
                'medical_history'=>$data['base']['medical_history'],
                'allergy_history'=>$data['base']['allergy_history'],
            ];
            $res = db('patient')->where('id', $id)->update($map);
            if($res){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
            return;
        }
        $patient = db('patient')->find($id);
        return $this->fetch('edit_base', array('userInfo'=>$patient));
    }

    //上传图片
    public function updateImg(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = $this->request->file('file');
        //dump($file);die;
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息

                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                //echo $info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                return  $this->success($info->getSaveName());
                //echo $info->getFilename();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
    }

    public function delete()
    {
        $id = $this->request->param('id', '', 'intval');
        $result = db('patient')->where('id', $id)->update(array('is_del'=>1));
        if (!$result){
            $this->error('删除失败');
        }
        $this->success('删除成功');
    }

}